﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kiel.Pdf
{
	class PdfStream : IPdfObject
	{
		private byte[] data;

		public PdfStream()
		{
			this.Header = new PdfDictionary();
		}

		public byte[] Data
		{
			get { return this.data; }
			set
			{
				this.data = value;
				this.Header[PdfName.Create("Length")] = new PdfNumeric(value.Length);
			}
		}

		/// <summary>
		/// Gets or sets the filters to be applied to this stream object.
		/// </summary>
		public PdfName[] Filters
		{
			get
			{
				return this.Header.ContainsKey("Filter") ? 
					((PdfArray)this.Header["Filter"]).Cast<PdfName>().ToArray() : new PdfName[0];
			}
			set { this.Header["Filter"] = new PdfArray(value); }
		}

		/// <summary>
		/// Gets the dictionary describing this stream object.
		/// </summary>
		public PdfDictionary Header { get; private set; }

		public override string ToString()
		{
			StringBuilder text = new StringBuilder();
			text.AppendFormat("{0}stream", this.Header.ToString());
			text.AppendLine();
			text.AppendLine(Encoding.UTF8.GetString(this.Data));
			text.AppendLine("endstream");
			return text.ToString();
		}
	}
}
